package com.huping.controller;


import com.alibaba.fastjson.JSONObject;
import com.huping.pojo.Course;
import com.huping.pojo.PageBean;
import com.huping.pojo.Result;
import com.huping.service.CourseService;
import com.huping.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
public class CourseController {

    @Autowired
    private CourseService courseService;

    @PostMapping ("/course/select")
    public Result select(@RequestHeader String token ,@RequestBody JSONObject param){
        Claims claims= JwtUtils.parseJWT(token);
        String kind = (String) claims.get("kind");
        PageBean pageBean;
        if (kind.equals("1")||kind.equals("2")){
            pageBean = courseService.courseSelect(claims,param);
            return Result.success(pageBean);
        }
        else
            return Result.error("未知身份");

    }

    @PostMapping("/course/create")
    public Result create(@RequestHeader String token, @RequestBody JSONObject param){
        Claims claims= JwtUtils.parseJWT(token);
        return courseService.create(param,claims);
    }

    @PostMapping("/course/join")
    public Result join(@RequestHeader String token,@RequestBody JSONObject param){
        Claims claims= JwtUtils.parseJWT(token);
        return courseService.join(param,claims);
    }

    @PostMapping("/course/detail")
    public Result detail(@RequestHeader String token,@RequestBody JSONObject param){
        Claims claims= JwtUtils.parseJWT(token);
        return courseService.detail(param,claims);
    }

    @PostMapping("/course/students")
    public Result students(@RequestHeader String token,@RequestBody JSONObject param ){
        Claims claims= JwtUtils.parseJWT(token);
        PageBean pageBean = courseService.students(param,claims);
        return Result.success(pageBean);
    }

    @PostMapping("/course/modify")
    public Result modify(@RequestBody JSONObject param){
        return courseService.modify(param);
    }


}
